<?php
/**
 * File from WEBO Video Transmuter, WEBO Software (http://www.webogroup.com/)
 * Provides config interface.
 *
 **/
class webo_video_transmuter_config {
/* constructor */
	function webo_video_transmuter_config () {
		$this->basepath = dirname(__FILE__) . '/../';
		$this->config = $this->basepath . 'config.wvt.php';
		$this->loadOptions();
	}
/* set an option */
	function setOption ($name, $value) {
		$this->options[$name] = $value;
	}
/* update configuration */
	function updateOptions() {
		$error = 0;
		$options = array('rootdir', 'videodir', 'host', 'api', 'ffmpeg', 'lang', 'email');
		foreach ($options as $option) {
			if (isset($_POST['wvt_' . $option])) {
				$this->setOption($option, htmlspecialchars($_POST['wvt_' . $option]));
			}
		}
		$this->setOption('host', preg_replace("!^https?://([^/]+).*!is", "$1", $this->options['host']));
/* test directories */
		if (!$this->testDirectory($this->options['videodir'])) {
			$error = 11;
		}
		if (!@shell_exec($this->options['ffmpeg'])) {
			$error = 12;
		}
		$this->saveOptions();
		return $error;
	}
/* save configuration */
	function saveOptions () {
		$content = "<?php\n";
		foreach ($this->options as $key => $value) {
			$content .= '$wvt_options["' . $key . '"]="' . $value . '";' . "\n";
		}
		@file_put_contents($this->config, $content);
	}
/* load configuration */
	function loadOptions () {
		require($this->config);
		$this->options = $wvt_options;
	}
/* test directory to write into */
	function testDirectory ($dir) {
		$tmp = $dir . 'tmp.file';
		@file_put_contents($tmp, '');
		if (!@is_file($tmp)) {
			return false;
		} else {
			@unlink($tmp);
			return true;
		}
	}
}